package com.runen.infinitemovement.tool;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


import com.runen.infinitemovement.R;

import java.util.List;


/**
 * Author: 廖端洪
 * E-mail: 178917169@qq.com
 * CreatedTime: 2019/7/9 9:35
 * <p>
 * CyWeatherUtils
 */
public class CyWeatherUtils {

    /**
     * 获取空气质量等级  0 优   5 严重污染
     *
     * @param aqi
     * @return
     */
    public static String getAirQuality(int aqi) {
        if (aqi < 50) {
            return "优";
        } else if (aqi < 100) {
            return "良";
        } else if (aqi <= 300) {
            return "差";
        } else {
            return "差";
        }
    }

//    public static int getAirQuality2(int aqi) {
//        if (aqi < 50) {
//            return R.drawable.background_25af4f_cr_dp2;
//        } else if (aqi < 100) {
//            return R.drawable.background_c7ad1e_cr_dp2;
//        } else if (aqi < 300) {
//            return R.drawable.background_c7691e_cr_dp2;
//        } else {
//            return R.drawable.background_e33e0b_cr_dp2;
//        }
//    }

    /**
     * 获取AQI对应的中文注释
     *
     * @param aqi
     * @return
     */
//    public static String getAirQualityStr(int aqi) {
//        return Flag.AIR_QUALITY[getAirQuality(aqi)];
//    }

    /**
     * 获取空气质量对应的颜色背景
     *
     * @param aqi aqi 值
     * @return int  drwable资源
     */
//    public static int getAirQualityCloer(int aqi) {
//        return ATR_QUALITY_BG[getAirQuality(aqi)];
//    }

    /**
     * 获取天气对应的中文字符
     *
     * @param skycon
     * @return
     */
    public static String getWeatherSkyconName(String skycon) {
        switch (skycon) {
            default:
            case WEATHER_STR.CLEAR_DAY:
                return "晴";
            case WEATHER_STR.CLEAR_NIGHT:
                return "晴";
            case WEATHER_STR.PARTLY_CLOUDY_DAY:
                return "多云";
            case WEATHER_STR.PARTLY_CLOUDY_NIGHT:
                return "多云";
            case WEATHER_STR.CLOUDY:
                return "阴";
            case WEATHER_STR.RAIN:
                return "雨";
            case WEATHER_STR.SNOW:
                return "雪";
            case WEATHER_STR.WIND:
                return "风";
        }
    }

    /**
     * CLEAR_DAY： 晴天
     * CLEAR_NIGHT：晴夜
     * PARTLY_CLOUDY_DAY：多云
     * PARTLY_CLOUDY_NIGHT：多云
     * CLOUDY：阴
     * RAIN： 雨
     * SNOW：雪
     * WIND：风
     * FOG：雾
     * HAZE：霾
     * SLEET：冻雨
     */
    public static abstract class WEATHER_STR {
        public static final String CLEAR_DAY = "CLEAR_DAY";
        public static final String CLEAR_NIGHT = "CLEAR_NIGHT";
        public static final String PARTLY_CLOUDY_DAY = "PARTLY_CLOUDY_DAY";
        public static final String PARTLY_CLOUDY_NIGHT = "PARTLY_CLOUDY_NIGHT";
        public static final String CLOUDY = "CLOUDY";
        public static final String RAIN = "RAIN";
        public static final String SNOW = "SNOW";
        public static final String WIND = "WIND";
        public static final String FOG = "FOG";
        public static final String HAZE = "HAZE";
        public static final String SLEET = "SLEET";
    }
    /**
     * 空气质量对应的背景   闹钟提醒时用到
     *
     * @param num
     * @return
     */
//    public static int getAirQualityBg(int num) {
//        switch (num) {
//            case 0:
//                return R.drawable.weather_icon1;
//            case 1:
//                return R.drawable.weather_icon2;
//            case 2:
//                return R.drawable.weather_icon3;
//            case 3:
//                return R.drawable.weather_icon4;
//            case 4:
//                return R.drawable.weather_icon5;
//            case 5:
//                return R.drawable.weather_icon6;
//            default:
//                return R.drawable.weather_icon1;
//        }
//    }

    /**
     * 获取实时天气图标
     *
     * @param weatherStr 彩云天气对应的天气值
     * @return
     */
    public static int getWeatherIcon(String weatherStr) {
        switch (weatherStr) {
            case WEATHER_STR.CLEAR_DAY:
                return R.mipmap.cy_clear_day;
            case WEATHER_STR.CLEAR_NIGHT:
                return R.mipmap.cy_clear_day;
            case WEATHER_STR.PARTLY_CLOUDY_DAY:
                return R.mipmap.cy_partly_cloudy_dy;
            case WEATHER_STR.PARTLY_CLOUDY_NIGHT:
                return R.mipmap.cy_partly_cloudy_dy;
            case WEATHER_STR.CLOUDY:
                return R.mipmap.cy_cloudy;
            case WEATHER_STR.RAIN:
                return R.mipmap.cy_rain;
            default:
                return R.mipmap.cy_cloudy;
        }
    }
    public static int getWeatherType(String weatherStr) {
        switch (weatherStr) {
            case WEATHER_STR.CLEAR_DAY:
                return 1;
            case WEATHER_STR.CLEAR_NIGHT:
                return 1;
            case WEATHER_STR.PARTLY_CLOUDY_DAY:
                return 2;
            case WEATHER_STR.PARTLY_CLOUDY_NIGHT:
                return 2;
            case WEATHER_STR.CLOUDY:
                return 3;
            case WEATHER_STR.RAIN:
                return 4;
            default:
                return 3;
        }
    }

    /**
     * 获取风力级数
     *
     * @param speed 风速  km/h
     * @return
     */
    public static int getWindLevel(double speed) {
        double sp = speed * 5 / 18;
        if (sp < 0.3)
            return 0;
        else if (sp < 1.6)
            return 1;
        else if (sp < 3.4)
            return 2;
        else if (sp < 5.5)
            return 3;
        else if (sp < 8.0)
            return 4;
        else if (sp < 10.8)
            return 5;
        else if (sp < 13.9)
            return 6;
        else if (sp < 17.2)
            return 7;
        else if (sp < 20.8)
            return 8;
        else if (sp < 24.5)
            return 9;
        else if (sp < 28.5)
            return 10;
        else if (sp < 32.7)
            return 11;
        else if (sp < 37.0)
            return 12;
        else if (sp < 41.5)
            return 13;
        else if (sp < 46.5)
            return 14;
        else if (sp < 51.0)
            return 15;
        else if (sp < 56.1)
            return 16;
        else if (sp < 61.3)
            return 17;
        else
            return 18;
    }

    /**
     * 获取风向
     *
     * @param angle 角度 0-360
     * @return
     */
    public static String getWindDirection(double angle) {
        if (angle >= 348.76 || angle <= 22.5)
            return "北";
        else if (angle <= 67.5)
            return "东北";
        else if (angle <= 112.5)
            return "东";
        else if (angle <= 157.5)
            return "东南";
        else if (angle <= 202.5)
            return "南";
        else if (angle <= 247.5)
            return "西南";
        else if (angle <= 292.5)
            return "西";
        else if (angle <= 337.5)
            return "西北";
        else
            return "北";
    }

    /**
     * 获取下雪强度
     *
     * @param snow 降雪强度   mm/h
     * @return
     */
    public static String getSnowLevel(double snow) {
        double s = snow * 12;
        if (s <= 1) {
            return "小雪";
        } else if (s < 3) {
            return "中雪";
        } else if (s < 6) {
            return "大雪";
        } else {
            return "暴雪";
        }
    }

    /**
     * 获取下雨等级
     *
     * @param rain 降雨强度  mm/h
     * @return
     */
    public static String getRainLevel(double rain) {
        double r = rain * 12;
        if (r < 5) {
            return "小雨";
        } else if (r < 15) {
            return "中雨";
        } else if (r < 30) {
            return "大雨";
        } else if (r < 70) {
            return "暴雨";
        } else if (r < 140) {
            return "大暴雨";
        } else {
            return "特大暴雨";
        }
    }

    public static String getRainLevelChange(List<Double> rain) {
        double minRain = 200.0;
        double maxRain = 0.0;
        for (int i = 0; i < rain.size(); i++) {
            double r = rain.get(i);
            if (r < minRain) {
                minRain = r;
            }
            if (r > maxRain) {
                maxRain = r;
            }
        }
        String le1 = getRainLevel(minRain);
        String le2 = getRainLevel(maxRain);
        if (le1.equals(le2)) {
            return le1;
        } else {
            return le1 + "转" + le2;
        }
    }

    public static String getAlertType(String code) {
        switch (code) {
            case "01":
                return "台风";
            case "02":
                return "暴雨";
            case "03":
                return "暴雪";
            case "04":
                return "寒潮";
            case "05":
                return "大风";
            case "06":
                return "沙尘暴";
            case "07":
                return "高温";
            case "08":
                return "干旱";
            case "09":
                return "雷电";
            case "10":
                return "冰雹";
            case "11":
                return "霜冻";
            case "12":
                return "大雾";
            case "13":
                return "雾霾";
            case "14":
                return "道路结冰";
            case "15":
                return "森林火灾";
            case "16":
                return "雷雨大风";
            default:
                return "";
        }
    }

    public static String getAlertLevel(String code) {
        switch (code) {
            case "01":
                return "蓝色";
            case "02":
                return "黄色";
            case "03":
                return "橙色";
            case "04":
                return "红色";
            default:
                return "";
        }
    }

    /**
     * 获取穿衣建议
     *
     * @param index 穿衣指数
     * @return
     */
    public static String getDress(int index) {
        switch (index) {
            case 1:
                return "轻棉织物轻棉织物制作的短衣、短裙、薄短裙、短裤";
            case 2:
                return "棉麻面料的衬衫、薄长裙、薄T恤";
            case 3:
                return "单层棉麻面料的短套装、T恤衫、薄牛仔衫裤、休闲服、职业套装";
            case 4:
                return "套装、夹衣、风衣、休闲装、夹克衫、西装、薄毛衣";
            case 5:
                return "风衣、大衣、夹大衣、外套、毛衣、毛套装、西装、防寒服";
            case 6:
                return "棉衣、冬大衣、皮夹克、外罩大衣、厚毛衣、皮帽皮手套、皮袄";
            case 7:
                return "棉衣、冬大衣、皮夹克、厚呢外套、呢帽、手套、羽绒服、裘皮大衣";
            default:
                return "";
        }
    }

    /**
     * 获取温度图标
     *
     * @param temp 温度
     * @return
     */
//    public static int[] getTemperatureIcons(int temp) {
//        int[] resorce = new int[4];
//        int i = 0;
//        if (temp < 0) {
//            resorce[i] = R.drawable.weather_num_min;
//            i++;
//        }
//        if (Math.abs(temp) < 10) {
//            resorce[i] = getTemperatureIcon(Math.abs(temp));
//            i++;
//        } else {
//            resorce[i] = getTemperatureIcon(Math.abs(temp) / 10);
//            resorce[i + 1] = getTemperatureIcon(Math.abs(temp) % 10);
//            i = i + 2;
//        }
//        resorce[i] = R.drawable.weather_num_unit;
//        i++;
//        int[] result = new int[i];
//        for (int j = 0; j < i; j++) {
//            result[j] = resorce[j];
//        }
//        return result;
//    }

//    private static int getTemperatureIcon(int num) {
//        switch (num) {
//            default:
//            case 0:
//                return R.drawable.weather_num_0;
//            case 1:
//                return R.drawable.weather_num_1;
//            case 2:
//                return R.drawable.weather_num_2;
//            case 3:
//                return R.drawable.weather_num_3;
//            case 4:
//                return R.drawable.weather_num_4;
//            case 5:
//                return R.drawable.weather_num_5;
//            case 6:
//                return R.drawable.weather_num_6;
//            case 7:
//                return R.drawable.weather_num_7;
//            case 8:
//                return R.drawable.weather_num_8;
//            case 9:
//                return R.drawable.weather_num_9;
//        }
//    }
//
//    public static int[] getForecastSuggestIcons() {
//        return new int[]{R.drawable.ic_weather, R.drawable.ic_warning, R.drawable.ic_live, R.drawable.ic_exercise_news};
//    }

    /**
     * @param latitude1  经度1
     * @param longitude1 纬度1
     * @param latitude2  经度2
     * @param longitude2 纬度2
     * @return 2个经纬度之间的距离 km
     */
    public static double getDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
        double Lat1 = rad(latitude1); // 纬度
        double Lat2 = rad(latitude2);
        double a = Lat1 - Lat2;//两点纬度之差
        double b = rad(longitude1) - rad(longitude2); //经度之差
        double s = 2 * Math.asin(Math
                .sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));//计算两点距离的公式
        s = s * 6378137.0;//弧长乘地球半径（半径为米）
        s = Math.round(s * 10000d) / 10000d;//精确距离的数值
        return s / 1000;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.00; //角度转换成弧度
    }


    /**
     * 将角度换算为弧度
     *
     * @param degrees 角度
     * @return 弧度
     */
    public static double ConvertDegreesToRadians(double degrees) {
        return degrees * Math.PI / 180;
    }

    public static double ConvertRadiansToDegrees(double radian) {
        return radian * 180.0 / Math.PI;
    }

    public static boolean isInterNet(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            int type = networkInfo.getType();
            if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }
        }
        return false;
    }
}
